"""
表格处理脚本

"""

import pymysql
import sys
from TableOne import Tableone
from TableTwo import Tabletwo
from analyze_table_pre1 import Table
from PubTable import Pubtable
from AchiTable import Achitable
from AnalyzeTablePre import Analyzetablepre
from conf1 import Conf1
from loguru import logger
import copy
from conf import *

class AnalyzeTable():
    def __init__(self,**params):
        self.html = params['html']
        self.config = params['config']
        self.database = params['database']
        self.config1 = params['config1']
        self.params1 = {'rubbish_word':params['rubbish_word'],
                        'replace_word': params['replace_word'],
                        'get_money': params['get_money'],
                        'config': params['config'],
                        'config1': params['config1']
                        }
        self.type = params['type']


    #获取十字数据
    def get_data_dict(self):
        data_dict_partone,record_key = Table(**self.params1).main(self.html,self.config['tag_ls'],self.database)
        return data_dict_partone,record_key

    def temp_part_two(self):
        params2 = {
            'html':self.html,
            'conf':self.config1,
            'config_dict':self.config1['config_dict'],
            'type':self.type
        }
        mul_ls = Analyzetablepre(**params2).main()
        params2['mul_ls'] = mul_ls
        pub_data_out = Pubtable(**params2).main()
        return pub_data_out
    #处理函数
    def data_handle(self,data_dict_partone,record_key):
        type_sec = self.config1['type_sec'][self.type]
        if self.type != '3':
            out_dict = {key: '' if key != '投标要求' else [] for key  in list(self.config1[type_sec].keys())}
        else:
            out_dict = {key: '' for key in list(self.config1['achivement_sec_common'].keys())}
            out_dict['第一中标候选人'] = []
            out_dict['第二中标候选人'] = []
            out_dict['第三中标候选人'] = []

        if self.type == '3':
            if not isinstance(data_dict_partone,str):
                part_one_data = Tableone(**self.params1).part_one_table(data_dict_partone, self.config['tag_ls'], record_key)
                if '第一中标候选人' in part_one_data:
                    out_dict['第一中标候选人'] = part_one_data['第一中标候选人']
        part_two_data = self.temp_part_two()
        for key in part_two_data:
            out_dict[key] = part_two_data[key]
        return out_dict

    def main(self):
        data_dict_partone,record_key = self.get_data_dict()
        end = self.data_handle(data_dict_partone,record_key)
        return end

def read_module ():

    conn = pymysql.connect(
            host='192.168.0.160',
            port=3306,
            user='root',
            passwd='lubanjianye',
            charset='utf8',
            database='data_collection_server',
            cursorclass=pymysql.cursors.DictCursor)
    cursor = conn.cursor()
    data_pass = []
    tag_pass = []
    data_dict = ''
    args = {'conn':conn,
            'cursor':cursor}
    conf = Conf(**args)
    config = conf.main()
    conf1 = Conf1(**args)
    config1 = conf1.main()
    confunc = ConFunc(config)
    rubbish_word = confunc.rubbish_word
    replace_word = confunc.replace_word
    get_money = confunc.get_money
    html = open('C:/Users/Administrator/Desktop/table_easy/table8.html',encoding='utf-8').read()
    params = {
        'html':html,
        'config':config,
        'rubbish_word':rubbish_word,
        'replace_word': replace_word,
        'get_money': get_money,
        'config1': config1,
        'type':'5',
        'database':True
    }
    read = AnalyzeTable(**params)
    end = read.main()
    return end

if __name__ == '__main__':
    print(read_module())